import random


def list2dict(column: list, result: list) -> dict:
    if not column or not result:
        raise RuntimeWarning("传入的参数不能为空")
    res_dict = {}
    for i, col in enumerate(column):
        if " : " in col:
            arr = col.split(" : ")
            pre_col_name = arr[0].strip()
            col_type = arr[1].strip()
        else:
            pre_col_name = col
            col_type = None
        if " as " in pre_col_name:
            col_name = pre_col_name.split(" as ")[1].strip()
        else:
            col_name = pre_col_name
        if col_type:
            if "int" == col_type:
                res_dict[col_name] = int(result[i]) if result[i] else 0
            elif "str" == col_type:
                res_dict[col_name] = str(result[i]) if result[i] else ''
            elif "float" == col_type:
                res_dict[col_name] = float(result[i]) if result[i] else 0.0
            else:
                res_dict[col_name] = result[i]
        else:
            res_dict[col_name] = result[i]
    return res_dict


def convert_list_2_map(original_list: list, key_index: int) -> dict:
    random_index = random.randint(0, len(original_list) - 1)
    random_row = original_list[random_index]
    if key_index > len(random_row):
        msg = f"{key_index} 不能超过 {original_list} 中元素的下标范围 "
        raise RuntimeError(msg)
    result_dict = {}
    for row in original_list:
        current_key = row[key_index]
        current_value = result_dict.get(current_key, None)
        if current_value:
            current_value.append(row)
        else:
            result_dict[current_key] = [row]
    return result_dict
